<?php
namespace app\order\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\Plugin;
use app\order\model\Order;
//use app\shop\model\Shop;
//use think;

/**
 * order 后台模块
 */
class Index extends Admin
{
    public function index()
    {
        $orderModel = new Order();
        $statusArr = $orderModel->getOrderStatus();

        $map = $this->getMap();
        $map['parent_id'] = 0;

        $order_status = input('order_status');
        if($order_status) $map['order_status'] = $order_status;

        $order_no = input('order_no');
        if($order_no) $map['order_no'] = $order_no;

        return ZBuilder::make('mix')
            ->setTableName('order')
            ->setPrimaryKey('order_id')
            //->setPageTitle('列表')
            ->addColumn('order_no', '订单号', 'link', url('info',['id'=>'__order_id__']))
            ->addColumn('create_time', '下单时间')
            ->addColumn('member_id', '会员')
            ->addColumn('order_id', '收货人', 'callback', function($value){
                $consignee = \think\Loader::model('OrderConsignee')->getConsignee($value);
                return $consignee['consignee'].' '.$consignee['mobile'];
            })
            ->addColumn('total_price', '总金额', 'callback', 'getPrice')
            ->addColumn('order_price', '应付金额', 'callback', 'getPrice')
            ->addColumn('warehouse_id', '仓库', 'callback', function($value){
                return Plugin::getWareHouseText($value);
            })
            ->addColumn('shop_id', '店铺', 'callback', function($value){
                //$data = Shop::getinstance()->getShopBaseInfo($value);
                $data = \think\Loader::model('app\shop\model\Shop')->getShopBaseInfo($value);
                return isset($data['name']) ? $data['name'] : '';
            })
            ->addColumn('order_type', '类型', 'callback', function($value){
                return Order::getOrderTypeText($value);
            })
            ->addColumn('pay_status', '支付状态', 'status', '', Order::getPayStatus())
            ->addColumn('shipping_status', '发货状态', 'status', '', Order::getShippingStatus())
            ->addColumn('order_status', '订单状态', 'status', '', Order::getOrderStatus())

            ->addSelect('order_status', '订单状态', '', $statusArr, $order_status)
            ->addText('order_no','订单号', '', $order_no)
            //->addSelect('member_id', '会员', '', '', '', 'ajax', '', ['url' => '/admin.php/capital/adjustment/adjustment_listtype/1', 'datakey' => 'id', 'dataname' => 'name'])
            
            //->setSearch(['order_no' => '订单号'])

            ->setRowList( $orderModel->search($map) )

            ->isAjax(false)
            ->setFormMethod('get')

            ->layout(['order_status'=>2,'order_no'=>2])

            ->fetch();
    }

    public function info()
    {
        $order_id = input('id');

        $data = model('Order')->info($order_id);
        $this->assign('data', $data);

        $this->assign('wareHouse', Plugin::getWareHouse());

        return $this->fetch('info');
    }

    public function test()
    {
        die('');
    }
}